package com.own.business.level.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.own.business.level.business.model.ExpModel;
import com.own.business.level.business.service.LevelUserHistoryService;
import com.own.business.level.entity.bo.LevelUserHistoryBo;
import com.own.business.level.entity.map.LevelUserHistoryMapVo;
import com.own.business.level.entity.po.LevelUser;
import com.own.business.level.entity.po.LevelUserHistory;
import com.own.business.level.entity.query.LevelUserHistoryQuery;
import com.own.business.level.entity.vo.LevelUserHistoryVo;
import com.own.business.level.mapper.LevelUserHistoryMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.component.common.login.user.BaseLoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * LevelUserHistoryServiceImpl
 *
 * @author chenxueli
 * @date 2022-07-25 10:45:00
 */
@Slf4j
@Service
public class LevelUserHistoryServiceImpl extends AbstractBaseService<
        LevelUserHistory,
        LevelUserHistoryBo,
        LevelUserHistoryVo,
        LevelUserHistoryMapVo,
        LevelUserHistoryQuery,
        LevelUserHistoryMapper
        > implements LevelUserHistoryService {

    /**
     * 添加流水信息
     *
     * @param userId    用户id
     * @param levelUser 用户经验信息
     * @param model     经验模型
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Long userId, LevelUser levelUser, ExpModel model) {
        var po = new LevelUserHistory();
        po.setFkUserId(userId);
        po.setExp(model.getExp());
        po.setBeforeExp(levelUser.getExp());
        po.setAfterExp(levelUser.getExp() + model.getExp());
        po.setType(model.getType());
        po.setOperateDate(LocalDate.now());
        po.setContent(model.getMessage());
        po.setConsecutive(model.getCount() + 1);
        add(po);
    }

    /**
     * 默认的排序信息
     *
     * @param wrapper 查询条件
     */
    @Override
    public void defaultSort(LambdaQueryWrapper<LevelUserHistory> wrapper) {
        super.defaultSort(wrapper);
        wrapper.orderByDesc(LevelUserHistory::getGmtCreate);
    }

    /**
     * 获取最新的数据信息
     *
     * @param userId 用户id
     * @param type   类型
     * @return 结果
     */
    @Override
    public LevelUserHistory last(Long userId, String type) {
        var wrapper = lambdaQueryWrapper()
                .eq(LevelUserHistory::getFkUserId, userId)
                .eq(LevelUserHistory::getType, type)
                .orderByDesc(LevelUserHistory::getGmtCreate);
        return getOneByWrapper(wrapper);
    }

    /**
     * 获取历史经验信息
     *
     * @param loginUser 登录的用户信息
     * @return 历史经验信息
     */
    @Override
    public List<LevelUserHistory> list(BaseLoginUser loginUser) {
        return null;
    }

}
